package cn.guoxuepeng.hrm.controller;

import cn.guoxuepeng.hrm.client.CacheClient;
import cn.guoxuepeng.hrm.client.SmsClient;
import cn.guoxuepeng.hrm.controller.vo.CodeVo;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.guoxuepeng.hrm.client.CacheClient;
import cn.guoxuepeng.hrm.util.AjaxResult;
import cn.hutool.core.util.RandomUtil;
import com.netflix.discovery.converters.Auto;
import com.netflix.discovery.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author solargen
 * @version v1.0.0
 * @date 2020-06-29
 */
@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private CacheClient cacheClient;

    @Autowired
    private SmsClient smsClient;


    /**
     * 加载图形验证码
     * 验证码有很多实现方式
     *  1、自己写
     *      不要钱，但是自己写的很low
     *  2、用别人提供的验证码服务
     *      高端大气上档次，但是要花钱
     *
     * @param uuid
     * @return
     */
    @GetMapping("/loadImageCode")
    public String loadImageCode(@RequestParam("uuid") String uuid){

        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);

        //获取生成的验证码
        String code = lineCaptcha.getCode();

        String imageBase64 = lineCaptcha.getImageBase64();

        //保存redis
        //key :   CODE:IMAGECODE:uuid
        String key = "CODE:IMAGECODE:"+uuid;
        AjaxResult ajaxResult = cacheClient.setex(key, 300, code);
        return imageBase64;

    }

    @PostMapping("/sendPhoneCode")
    public AjaxResult sendPhoneCode(@RequestBody CodeVo codeVo){
        try {
            if(codeVo.getCode()==null&&codeVo.getPhoneNum()==null&&codeVo.getUuid()==null){
                return AjaxResult.me().setSuccess(false).setMessage("输入的信息有误");
            }
            AjaxResult ajaxResult = cacheClient.get("CODE:IMAGECODE:" + codeVo.getUuid());
            String code = (String) ajaxResult.getResultObj();
            if(code==null&&!code.equals(codeVo.getCode())){
                return AjaxResult.me().setSuccess(false).setMessage("验证码错误");
            }
            AjaxResult ajaxResult1 = cacheClient.get("CODE:REGCODE:" + codeVo.getPhoneNum());
            String regCode = (String) ajaxResult1.getResultObj();
            if(regCode!=null){
                String[] split = regCode.split(",");
                Long time = Long.valueOf(split[1]);
                if(time+60L*1000L>=System.currentTimeMillis()){
                    return AjaxResult.me().setSuccess(false).setMessage("非法请求，我以报警");
                }
            }
            String s = RandomUtil.randomString("1234567890", 6);
            AjaxResult ajaxResult2 = cacheClient.setex("CODE:REGCODE:" + codeVo.getPhoneNum(), 300, s + "," + System.currentTimeMillis());
            if (ajaxResult2.getSuccess()){
                //发短信要钱，平常就要输出语句测试
                //smsClient.sendRegCode(codeVo.getPhoneNum(),s);
                System.out.println("发送成功");
            }
            return AjaxResult.me().setSuccess(true).setMessage("成功");
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("失败"+e.getMessage());
        }
    }


    public static void main(String[] args) {
        Long l=1593480216237L;
        System.out.println(l+60L*1000L>=System.currentTimeMillis());
        System.out.println(l+60L*1000L);
        System.out.println(System.currentTimeMillis());
    }




}
